﻿using System;
using TierApplication;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;

public partial class ForeProgram_shopAddOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadUser();
            LoadCart();
            TotalCart();
        }
    }

    #region 加载用户信息
    /// <summary>
    /// 加载用户信息
    /// </summary>
    private void LoadUser()
    {
        T_ACCOUNT account = new T_ACCOUNT();
        DataSet ds = account.GetAccount(Security.GetAccountId());
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count != 0)
        {
            lblName.Text = ds.Tables[0].Rows[0]["ACCOUNTNAME"].ToString();
            lblEMail.Text = ds.Tables[0].Rows[0]["EMAIL"].ToString();
            lblMobile.Text = ds.Tables[0].Rows[0]["MOBILE"].ToString();
            lblSchoolName.Text = ds.Tables[0].Rows[0]["SchoolName"].ToString();
        }
    }
    #endregion

    #region 加载数据
    /// <summary>
    /// 加载数据
    /// </summary>
    private void LoadCart()
    {
        T_CART cart = new T_CART();
        string strWhere = " And accountid='" + Security.GetAccountId() + "'";
        gvTable.DataSource = cart.GetCartByWhere(strWhere).Tables[0];
        gvTable.DataBind();
    }
    #endregion

    #region 事件
    protected void gvTable_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.DataItemIndex == -1) return;
        HyperLink hlProduct = (HyperLink)e.Row.FindControl("hlProduct");
        hlProduct.NavigateUrl = "ProductView.aspx?productid=" + DataBinder.Eval(e.Row.DataItem, "productid").ToString();
        hlProduct.Text = DataBinder.Eval(e.Row.DataItem, "Productname").ToString();
    }
    #endregion

    #region 统计购物车
    /// <summary>
    /// 统计购物车
    /// </summary>
    private void TotalCart()
    {
        Clear();
        if (gvTable != null)
        {
            decimal amount = 0;
            for (int i = 0; i < gvTable.Rows.Count; i++)
            {
                
                string price = gvTable.Rows[i].Cells[3].Text;
                string quantity = gvTable.Rows[i].Cells[5].Text;
                amount += Convert.ToInt32(quantity) * Convert.ToDecimal(price);
            }
            lblAmount.Text = "￥" + amount.ToString();
            lbltotalAmount.Text = lblAmount.Text;
        }
    }
    #endregion

    #region 清空
    private void Clear()
    {
        lblAmount.Text = "￥0.00";
        lbltotalAmount.Text = "￥0.00";
    }
    #endregion

    #region 订单生成
    /// <summary>
    /// 订单生成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        T_ACCOUNT account = new T_ACCOUNT(Security.GetAccountId());
        if (account.MOBILE != txtPhone.Text)
        {
            lblVerifier.Text = "手机号输入错误";
            return;
        }
        T_ORDER order = new T_ORDER();
        int ai_result=-1;
        string ac_orderid=string.Empty;
        string ac_result=string.Empty;
        try
        {
            order.SELLCARD(Security.GetAccountId(), ref ai_result, ref ac_orderid, ref ac_result);
            if (ai_result == 0)
            {
                Context.Items.Add("OrderId", ac_orderid);
                Context.Items.Add("ai_result", ai_result);
                Context.Items.Add("ac_result", ac_result);
                
                Server.Transfer("shopSaveOrder.aspx");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('" + ac_result + "！');swithBtn('display');</script>");
            }
        }
        catch (Exception ex)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('交易失败！');swithBtn('display');</script>");
        }
    }
    #endregion
}